package com.author.surface;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.FontRenderContext;

import javax.swing.JToggleButton;
import javax.swing.border.BevelBorder;

import com.author.document.style.StyleChangeNotify;

public class LineSpacingButtonGroup {
	/** The three buttons for line spacing - single, one half and double */
	private final static JToggleButton	singleSpace		= new SingleSpace();
	private final static JToggleButton	onehalfSpace	= new OneHalfSpace();
	private final static JToggleButton	doubleSpace		= new DoubleSpace();

	public static SingleSpace getSingleSpaceButton() {
		return (SingleSpace) singleSpace;
	}

	public static OneHalfSpace getOneHalfSpaceButton() {
		return (OneHalfSpace) onehalfSpace;
	}

	public static DoubleSpace getDoubleSpaceButton() {
		return (DoubleSpace) doubleSpace;
	}

	public static class SingleSpace extends JToggleButton implements
			ActionListener {
		/** X and Y coordinate of the visible lines */
		private int					x1, x2;
		private int					y1, y2;

		/** display properties of the line spacing action buttons */
		private static final int	LINESPACE_BUTTON_WIDTH		= 32;
		private static final int	LINESPACE_BUTTON_HEIGHT		= 32;
		private static final int	LINESPACE_BUTTON_FONT_SIZE	= 24;

		/** Font used on the line spacing buttons */
		private Font				singleLineSpaceButtonFont	= new Font(
																		fontName,
																		Font.PLAIN,
																		LINESPACE_BUTTON_FONT_SIZE);
		private static final String	fontName					= "Monospaced";

		/** Line space for SingleSpace lines */
		public static final float	SINGLE_SPACING				= (float) 0.25;

		/** Current state of the Single Line Space button */
		private boolean				currentState				= false;

		public SingleSpace() {
			super();
			// Set size of the SINGLE SPACE button
			setSize(new Dimension(LINESPACE_BUTTON_WIDTH,
					LINESPACE_BUTTON_HEIGHT));
			setPreferredSize(getSize());
			setEnabled(true);

			// Set border of the SINGLE SPACE button
			setBorder(new BevelBorder(BevelBorder.LOWERED));
			setBorderPainted(false);

			// Set visible properties
			setFont(singleLineSpaceButtonFont);
			setToolTipText("Single Space");
			setFocusable(false);
			setVisible(true);

			// Make the SINGLE SPACE button respond to user click action
			addActionListener(this);
		}

		/**
		 * This method will be executed in response to the message from UI
		 * updater which will request the line space button to check if it needs
		 * to update its state based on the current caret position
		 */
		public void setSingleLineSpace(boolean value) {
			if (value == true) {
				if (!isSelected()) {
					setSelected(true);
					currentState = true;
				}
			}

			if (value == false) {
				if (isSelected()) {
					setSelected(false);
					currentState = false;
				}
			}
		}

		public void paint(Graphics g) {
			super.paint(g);
			Graphics2D g2d = (Graphics2D) g;
			g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);

			x1 = 5;
			y1 = 5;
			x2 = 25;
			y2 = 5;
			g2d.drawLine(x1, y1, x2, y2);

			x1 = 5;
			y1 = 7;
			x2 = 25;
			y2 = 7;
			g2d.drawLine(x1, y1, x2, y2);

			x1 = 5;
			y1 = 9;
			x2 = 25;
			y2 = 9;
			g2d.drawLine(x1, y1, x2, y2);

			x1 = 5;
			y1 = 11;
			x2 = 25;
			y2 = 11;
			g2d.drawLine(x1, y1, x2, y2);

			x1 = 5;
			y1 = 13;
			x2 = 25;
			y2 = 13;
			g2d.drawLine(x1, y1, x2, y2);

			x1 = 5;
			y1 = 15;
			x2 = 25;
			y2 = 15;
			g2d.drawLine(x1, y1, x2, y2);

			x1 = 5;
			y1 = 17;
			x2 = 25;
			y2 = 17;
			g2d.drawLine(x1, y1, x2, y2);

			x1 = 5;
			y1 = 19;
			x2 = 25;
			y2 = 19;
			g2d.drawLine(x1, y1, x2, y2);

			x1 = 5;
			y1 = 21;
			x2 = 25;
			y2 = 21;
			g2d.drawLine(x1, y1, x2, y2);

			x1 = 5;
			y1 = 23;
			x2 = 25;
			y2 = 23;
			g2d.drawLine(x1, y1, x2, y2);

			x1 = 5;
			y1 = 25;
			x2 = 25;
			y2 = 25;
			g2d.drawLine(x1, y1, x2, y2);
		}

		@Override
		public void actionPerformed(ActionEvent event) {
			if (event == null) return;
			if (event.getSource() instanceof JToggleButton) {
				JToggleButton button = (JToggleButton) event.getSource();
				StyleChangeNotify styleChangeNotify = StyleChangeNotify
						.getStyleChangeNotify();

				if (button.isSelected()) {
					JToggleButton button1 = LineSpacingButtonGroup.onehalfSpace;
					JToggleButton button2 = LineSpacingButtonGroup.doubleSpace;

					if (button1.isSelected()) {
						styleChangeNotify.setOneHalfSpace(true, false);
						button1.setSelected(false);
					}

					if (button2.isSelected()) {
						styleChangeNotify.setDoubleSpace(true, false);
						button2.setSelected(false);
					}

					// update line spacing
					currentState = true;
					styleChangeNotify.setSingleSpace(false, true);
				} else {
					currentState = false;
					styleChangeNotify.setSingleSpace(true, false);
				}
			}
		}
	}

	public static class OneHalfSpace extends JToggleButton implements
			ActionListener {
		/** X and Y coordinate of the visible lines */
		private int					x1, x2;
		private int					y1, y2;

		/** display properties of the line spacing action buttons */
		private static final int	LINESPACE_BUTTON_WIDTH		= 32;
		private static final int	LINESPACE_BUTTON_HEIGHT		= 32;
		private static final int	LINESPACE_BUTTON_FONT_SIZE	= 24;

		/** Font used on the line spacing buttons */
		private Font				oneHalfLineSpaceButtonFont	= new Font(
																		fontName,
																		Font.PLAIN,
																		LINESPACE_BUTTON_FONT_SIZE);
		private static final String	fontName					= "Monospaced";

		/** Line space for OneHalfSpace lines */
		public static final float	ONEHALF_SPACING				= (float) 0.5;

		/** Current state of the One Half Line Space button */
		private boolean				currentState				= false;

		public OneHalfSpace() {
			super();
			// Set size of the ONE HALF SPACE button
			setSize(new Dimension(LINESPACE_BUTTON_WIDTH,
					LINESPACE_BUTTON_HEIGHT));
			setPreferredSize(getSize());
			setEnabled(true);

			// Set border of the ONE HALF SPACE button
			setBorder(new BevelBorder(BevelBorder.LOWERED));
			setBorderPainted(false);

			// Set visible properties
			setFont(oneHalfLineSpaceButtonFont);
			setToolTipText("One Half Space");
			setFocusable(false);
			setVisible(true);

			// Make the ONE HALF SPACE button respond to user click action
			addActionListener(this);
		}

		/**
		 * This method will be executed in response to the message from UI
		 * updater which will request the line space button to check if it needs
		 * to update its state based on the current caret position
		 */
		public void setOneHalfLineSpace(boolean value) {
			if (value == true) {
				if (!isSelected()) {
					setSelected(true);
					currentState = true;
				}
			}

			if (value == false) {
				if (isSelected()) {
					setSelected(false);
					currentState = false;
				}
			}
		}

		public void paint(Graphics g) {
			super.paint(g);
			Graphics2D g2d = (Graphics2D) g;
			g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);

			x1 = 5;
			y1 = 5;
			x2 = 25;
			y2 = 5;
			g2d.drawLine(x1, y1, x2, y2);

			x1 = 5;
			y1 = 9;
			x2 = 25;
			y2 = 9;
			g2d.drawLine(x1, y1, x2, y2);

			x1 = 5;
			y1 = 13;
			x2 = 25;
			y2 = 13;
			g2d.drawLine(x1, y1, x2, y2);

			x1 = 5;
			y1 = 17;
			x2 = 25;
			y2 = 17;
			g2d.drawLine(x1, y1, x2, y2);

			x1 = 5;
			y1 = 21;
			x2 = 25;
			y2 = 21;
			g2d.drawLine(x1, y1, x2, y2);

			x1 = 5;
			y1 = 25;
			x2 = 25;
			y2 = 25;
			g2d.drawLine(x1, y1, x2, y2);
		}

		@Override
		public void actionPerformed(ActionEvent event) {
			if (event == null) return;
			if (event.getSource() instanceof JToggleButton) {
				JToggleButton button = (JToggleButton) event.getSource();
				StyleChangeNotify styleChangeNotify = StyleChangeNotify
						.getStyleChangeNotify();

				if (button.isSelected()) {
					JToggleButton button1 = LineSpacingButtonGroup.singleSpace;
					JToggleButton button2 = LineSpacingButtonGroup.doubleSpace;

					if (button1.isSelected()) {
						styleChangeNotify.setSingleSpace(true, false);
						button1.setSelected(false);
					}

					if (button2.isSelected()) {
						styleChangeNotify.setDoubleSpace(true, false);
						button2.setSelected(false);
					}

					// update line spacing
					currentState = true;
					styleChangeNotify.setOneHalfSpace(false, true);
				} else {
					currentState = false;
					styleChangeNotify.setOneHalfSpace(true, false);
				}
			}
		}
	}

	public static class DoubleSpace extends JToggleButton implements
			ActionListener {
		/** X and Y coordinate of the visible lines */
		private int					x1, x2;
		private int					y1, y2;

		/** display properties of the line spacing action buttons */
		private static final int	LINESPACE_BUTTON_WIDTH		= 32;
		private static final int	LINESPACE_BUTTON_HEIGHT		= 32;
		private static final int	LINESPACE_BUTTON_FONT_SIZE	= 24;

		/** Font used on the line spacing buttons */
		private Font				doubleLineSpaceButtonFont	= new Font(
																		fontName,
																		Font.PLAIN,
																		LINESPACE_BUTTON_FONT_SIZE);
		private static final String	fontName					= "Monospaced";

		/** Line space for DoubleSpace lines */
		public static final float	DOUBLE_SPACING				= (float) 1.0;

		/** Current state of the Double Line Space button */
		private boolean				currentState				= false;

		public DoubleSpace() {
			super();
			// Set size of the DOUBLE SPACE button
			setSize(new Dimension(LINESPACE_BUTTON_WIDTH,
					LINESPACE_BUTTON_HEIGHT));
			setPreferredSize(getSize());
			setEnabled(true);

			// Set border of the DOUBLE SPACE button
			setBorder(new BevelBorder(BevelBorder.LOWERED));
			setBorderPainted(false);

			// Set visible properties
			setFont(doubleLineSpaceButtonFont);
			setToolTipText("Double Space");
			setFocusable(false);
			setVisible(true);

			// Make the DOUBLE SPACE button respond to user click action
			addActionListener(this);
		}

		/**
		 * This method will be executed in response to the message from UI
		 * updater which will request the line space button to check if it needs
		 * to update its state based on the current caret position
		 */
		public void setDoubleLineSpace(boolean value) {
			if (value == true) {
				if (!isSelected()) {
					setSelected(true);
					currentState = true;
				}
			}

			if (value == false) {
				if (isSelected()) {
					setSelected(false);
					currentState = false;
				}
			}
		}

		public void paint(Graphics g) {
			super.paint(g);
			Graphics2D g2d = (Graphics2D) g;
			g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);

			x1 = 5;
			y1 = 5;
			x2 = 25;
			y2 = 5;
			g2d.drawLine(x1, y1, x2, y2);

			x1 = 5;
			y1 = 15;
			x2 = 25;
			y2 = 15;
			g2d.drawLine(x1, y1, x2, y2);

			x1 = 5;
			y1 = 25;
			x2 = 25;
			y2 = 25;
			g2d.drawLine(x1, y1, x2, y2);
		}

		@Override
		public void actionPerformed(ActionEvent event) {
			if (event == null) return;
			if (event.getSource() instanceof JToggleButton) {
				JToggleButton button = (JToggleButton) event.getSource();
				StyleChangeNotify styleChangeNotify = StyleChangeNotify
						.getStyleChangeNotify();

				if (button.isSelected()) {
					JToggleButton button1 = LineSpacingButtonGroup.singleSpace;
					JToggleButton button2 = LineSpacingButtonGroup.onehalfSpace;

					if (button1.isSelected()) {
						styleChangeNotify.setSingleSpace(true, false);
						button1.setSelected(false);
					}

					if (button2.isSelected()) {
						styleChangeNotify.setOneHalfSpace(true, false);
						button2.setSelected(false);
					}

					// update line spacing
					currentState = true;
					styleChangeNotify.setDoubleSpace(false, true);
				} else {
					currentState = false;
					styleChangeNotify.setDoubleSpace(true, false);
				}
			}
		}

	}

}
